package cn.snmp.mutual.handler;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Vector;

import cn.snmp.mutual.constants.NnTempLetError;
import cn.snmp.mutual.nms.ErrorMessages;
import cn.snmp.mutual.nms.SnmpVar;
import cn.snmp.mutual.nms.SnmpVarBind;
import lombok.extern.slf4j.Slf4j;
import cn.snmp.mutual.data.ObjService;
import cn.snmp.mutual.common.snmp.SnmpNioTarget;
import cn.snmp.mutual.util.DumpUtil;
import cn.snmp.mutual.util.OperatorUtilities;
/**
 * @author dengkn
 * GetList
 * */
@Slf4j
public class SnmpGetList {

	/**
	 * 读取List类型的表的数据参照NETemplate.SnmpGetList编译部分
	 * @param snmpTarget  SnmpTarget实例
	 * @param snmpTarget
	 * @param osResult 表结构
	 */
	public static void hander(SnmpNioTarget snmpTarget,ObjService osCommandParams, ObjService osResult) {
		try {
			if(osCommandParams.getValue("Instance") == null || "".equals(osCommandParams.getValue("Instance")) || osCommandParams.objectAt("RowSet", 0).getValue("Instance")== null || "".equals(osCommandParams.objectAt("RowSet", 0).getValue("Instance"))) {
				osCommandParams.setValue("Instance", "0");
			}
			snmpTarget.setSNMPOpType(SnmpNioTarget.SNMP_GET_LIST);
			ObjService osMibNodeOIDTable = new ObjService();
			Vector<String> vNodeOIDVector = new Vector<String>();
			for (int i = 0; i < osResult.objectSize("MibNode"); i++) {
				ObjService osMibNode = osResult.objectAt("MibNode", i);

				String strOID = osMibNode.getStringValue("OID");
				if (strOID.equals(""))
					continue;
				strOID = strOID + "." + osCommandParams.getStringValue("Instance");
				
				String strIsExists = osMibNodeOIDTable.getStringValue(strOID);
				if (!strIsExists.equalsIgnoreCase("true")) {
					osMibNodeOIDTable.setValue(strOID, "true");
					vNodeOIDVector.add(strOID);
				}
			}
			if (vNodeOIDVector.size() == 0) {
				log.debug("not found mutual mib node!");
				snmpTarget.setFinishTag(true);
				osResult.setValue("ErrCode", ""+ NnTempLetError.SNMP_NODE_NOT_FOUND);
				osResult.setValue("isFinish", "true");
				osResult.setValue("ErrDesc", "SNMP_NODE_NOT_FOUND");
				return;
			}

			String[] strOIDs = new String[vNodeOIDVector.size()];
			for (int i = 0; i < vNodeOIDVector.size(); i++)
				strOIDs[i] = vNodeOIDVector.get(i);
			snmpTarget.setObjectIDList(strOIDs);
			if ((strOIDs == null) || (strOIDs.length < 1)) {
				snmpTarget.setFinishTag(true);
				snmpTarget.setErrCode(21);
				snmpTarget.setErrDes(ErrorMessages.getErrorString(21));
			}
		} catch (Exception e) {
			log.error("hander mutual operator error!",  e);
			DumpUtil.debug(e);
			osResult.setValue("ErrCode", "801");
			osResult.setValue("ErrDesc", e.getMessage());
			snmpTarget.setFinishTag(true);
		}
	}
	
	/**
	 * @param target     返回结果
	 * @return ObjService 最终结果
	 */
	public static void parser(SnmpNioTarget target) {
		try {
			ObjService params = (ObjService) target.getOsParam();
			ArrayList<SnmpVarBind> orgResult = (ArrayList<SnmpVarBind>) target.getOrgResult();
			ObjService result = target.getResult();
			result.setValue("isFinish", "true");

			if (orgResult.size() != target.getOidList().length) {
				result.setValue("ErrCode", "" + NnTempLetError.AGENT_REPONSE_ERROR);
				result.setValue("ErrDesc", NnTempLetError.getErrorMessage(NnTempLetError.AGENT_REPONSE_ERROR));
				return;
			}
			if (orgResult.size() == 0) {
				result.setValue("ErrCode", "2");
				result.setValue("ErrDesc", "List is null");
				return;
			}
			HashMap<String, SnmpVar> htBindTable = new HashMap<String, SnmpVar>();
			for (int i = 0; i < orgResult.size(); i++) {
				SnmpVarBind tvb = orgResult.get(i);
				htBindTable.put(tvb.getObjectID().toString(), tvb.getVariable());
			}
			ObjService osTemp = new ObjService("RowSet");
			for (int i = 0; i < result.objectSize("MibNode"); i++) {
				ObjService osNode = result.objectAt("MibNode", i);
				if (!osNode.getStringValue("Type").equalsIgnoreCase(""))
					continue;
				try {
					String strOID = osNode.getStringValue("OID");
					strOID = strOID + "." + params.getStringValue("Instance");
					String logicName = osNode.getStringValue("Logic");
					SnmpVar value = (SnmpVar) htBindTable.get(strOID);
					HashMap table = osTemp.getCurrentHashMap();
					table.put(logicName, OperatorUtilities.readSnmpVar(value, osNode, target.getOsParam(), osTemp));
				} catch (Exception e) {
					e.printStackTrace();
					result.setValue("ErrCode", "" + NnTempLetError.MIBNODEADAPTER_READ_ERROR);
					result.setValue("isFinish", "true");
					result.setValue("ErrDesc", e.toString());
					return;
				}
			}
			result.addContainedObject(osTemp);
			result.setValue("ErrCode", "0");
			result.setValue("isFinish", "true");
			result.setValue("ErrDesc", "Get List Success");
		} catch (Exception e) {
			log.error("mutual get list parser data error!", e);
			target.getResult().setValue("ErrCode", "801");
			target.getResult().setValue("ErrDesc", e.getMessage());
		}
	}
}
